package com.ji.li.reactadminproexampleservice.entity.user;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ji.li.reactadminproexampleservice.entity.auth.AuthRole;
import com.ji.li.reactadminproexampleservice.entity.auth.AuthUserRole;
import com.ji.li.reactadminproexampleservice.entity.menu.Menu;
import com.ji.li.reactadminproexampleservice.entity.menu.Role;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户表
 */
// mybatis-plus 注解表名
@TableName(value = "user_table")
public class User {
    @TableId(value = "u_id",type = IdType.AUTO)
    private Integer uId = 0;
    @TableField(value = "user_name")
    private String userName = "";
    @TableField(value = "pass_word")
    private String passWord = "";
    @TableField(value = "header")
    private String header = "";
    /**
     * false 代表 禁止 该用户登录
     */
    @TableField(value = "enabled")
    private Boolean enabled;
    /**
     * 这个用户 拥有 哪些接口权限（角色组）
     */
    private List<AuthUserRole> authUserRoles = new ArrayList<>();
    /**
     * 这个用户 拥有 哪些菜单权限（角色组）
     */
    private List<Role> role = new ArrayList<>();
    private List<Menu> menu = new ArrayList<>();

    public List<SimpleGrantedAuthority> getRoles() {
        List<AuthRole> authRoles = authUserRoles.stream().map(AuthUserRole::getRole).collect(Collectors.toList());
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        authRoles.forEach(role -> authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName())));
        return authorities;
    }

    public String getUserName() {
        return userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public Integer getuId() {
        return uId;
    }

    public void setuId(Integer uId) {
        this.uId = uId;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public List<AuthUserRole> getUserRoles() {
        return authUserRoles;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public void setUserRoles(List<AuthUserRole> authUserRoles) {
        this.authUserRoles = authUserRoles;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public List<Role> getRole() {
        return role;
    }

    public void setRole(List<Role> role) {
        this.role = role;
    }

    public List<Menu> getMenu() {
        return menu;
    }

    public void setMenu(List<Menu> menu) {
        this.menu = menu;
    }

    public List<AuthUserRole> getAuthUserRoles() {
        return authUserRoles;
    }

    public void setAuthUserRoles(List<AuthUserRole> authUserRoles) {
        this.authUserRoles = authUserRoles;
    }

    @Override
    public String toString() {
        return "User{" +
                "uId=" + uId +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                ", header='" + header + '\'' +
                ", enabled=" + enabled +
                ", authUserRoles=" + authUserRoles +
                ", role=" + role +
                ", menu=" + menu +
                '}';
    }
}
